<?php
// 命名空间定义
namespace app\api\controller;
 
use app\BaseController;
use think\facade\Config;
use think\facade\Request;  
use app\common\service\UploadService;
use EasyAdmin\upload\Uploadfile;
use think\facade\Cache;

class Upload extends BaseController
{
    public function uploadImage()  
    {  
        // oss 上传
        // if (!empty($this->request->param('debug'))) {
            $data = [
                'upload_type' => "txcos",
                'file'        => $this->request->file('file'),
            ];
            $uploadConfig = $this->sysconfig('upload');
            empty($data['upload_type']) && $data['upload_type'] = $uploadConfig['upload_type'];
            $rule = [
                'upload_type|指定上传类型有误' => "in:{$uploadConfig['upload_allow_type']}",
                'file|文件'              => "require|file|fileExt:{$uploadConfig['upload_allow_ext']}|fileSize:{$uploadConfig['upload_allow_size']}",
            ];
            $this->validate($data, $rule);
            try {
                $upload = Uploadfile::instance()
                    ->setUploadType($data['upload_type'])
                    ->setUploadConfig($uploadConfig)
                    ->setFile($data['file'])
                    ->save();
                return msg(200,'上传成功',['image_url'=>$upload['url']]);
            } catch (\Exception $e) {
                return msg(100,'上传失败','');
            }
        // }
        // // 获取上传的文件  
        // $file = Request::file('file');  
        // // 验证文件是否存在  
        // if (empty($file)) {  
        //     return msg(100,'请上传文件','');
        // }  
  
        // // 定义允许的文件格式  jpeg jpg png gif bmp TIFF|tiff PSD|psd ico icon
        // $allowedMimeTypes = ['image/jpeg','image/jpg','image/png','image/gif','image/bmp','image/ico','image/icon'];
        // // 获取文件的 MIME 类型  
        // $mimeType = $file->getMime();  
        // // 检查 MIME 类型是否在允许的列表中  
        // if (!in_array($mimeType, $allowedMimeTypes)) {  
        //     return msg(100,'请上传有效文件: '.'jpeg|jpg|png|gif|ico|icon|bmp','');
        // }  

        // // 定义最大文件大小（例如：2MB）  10MB限制
        // $maxSize = 10 * 1024 * 1024; // 1MB in bytes  
        // // 获取文件大小  
        // $fileSize = $file->getSize();  
        // // 检查文件大小是否超过限制  
        // if ($fileSize > $maxSize) {  
        //     return msg(100,'请上传有效文件: '.'文件限于1MB之内','');
        // }  
  
        // $image = date('YmdHis',time()) .rand(1000,9999).'.jpg';
        // // 移动文件到指定目录  
        // $info = $file->move('uploads' ,$image);  
        // if ($info) {  
        //     // 成功上传后获取文件信息  
        //     $filePath = $info->getFileName();  
        //     $web_url = Config::get('app')['const_data']['web_url'];
        //     $image_url = $this->request->domain() .'/uploads/'.$filePath;
        //     return msg(200,'文件上传成功',['image_url'=>$image_url]);
        // } else {  
        //     // 上传失败  
        //     return msg(100,'文件上传失败','');
        // }  
    }  

    function sysconfig($group, $name = null)
        {
            $where = ['group' => $group];
            $value = empty($name) ? Cache::get("sysconfig_{$group}") : Cache::get("sysconfig_{$group}_{$name}");
            if (empty($value)) {
                if (!empty($name)) {
                    $where['name'] = $name;
                    $value = \app\admin\model\SystemConfig::where($where)->value('value');
                    Cache::tag('sysconfig')->set("sysconfig_{$group}_{$name}", $value, 3600);
                } else {
                    $value = \app\admin\model\SystemConfig::where($where)->column('value', 'name');
                    Cache::tag('sysconfig')->set("sysconfig_{$group}", $value, 3600);
                }
            }
            return $value;
        }
}